import qcloud from 'wafer2-client-sdk'
// import {showSuccess} from './fetch.js'
import {baseUrl} from './env'
/**
 * 微信授权
 */
// 获取验证
export const obtainAccreditLoginFlag = () => {
  return new Promise((resolve, reject) => {
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          reject(false)
        } else {
          // 首次登录
          qcloud.setLoginUrl(baseUrl + '/weapp/login')
          qcloud.login({
            success: res => {
              // showSuccess("登录成功");
              wx.setStorageSync('userInfo', res)
              console.log('登录成功', res)
              resolve(true)
            },
            fail: err => {
              console.error(err)
              reject(false)
            }
          })
        }
      }
    })

  })
}
/**
 * 信息提示框
 */
export const showMessager = (text) => {
  wx.showToast({
    title: text,
    icon: 'none'
  })
}
/**
 * 微信支付
 */
export const wechatPayment = (payModel) => {
  return new Promise((resolve, reject) => {
    wx.requestPayment({
      'timeStamp': payModel.timestamp,
      'nonceStr': payModel.nonceStr,
      'package': payModel.package,
      'signType': 'MD5',
      'paySign': payModel.paySign,
      'success': function (res) {
        showMessager('支付成功')
        resolve(true)
      },
      'fail': function (res) {
        console.log(res, '支付失败')
        showMessager('支付失败')
        resolve(false)
      }
    })
  })
}

/**
 * 展示图片墙
 */
export const showPictureCard = (currentImage, imageList) => {
  wx.previewImage({
    current: currentImage, // 当前显示图片的http链接
    urls: imageList // 需要预览的图片http链接列表
  })
}

/**
 * 获取设置位置是否授权获取设置位置是否授权, type为查看什么类型的授权
 */
export const obtainLocationFlag = (type) => {
  return new Promise( (resolve, reject) => {
    //判断是否获得了用户地理位置授权
    wx.getSetting({
      success: (res) => {
        console.log('获取设置位置是否授权',res,type)
        if (!res.authSetting[type]) {
          resolve(false)
        } else {
          resolve(true)
        }
      }
    })
  })
}

/**
 * 获取位置数据
 */
export const obtainLocationData = () => {
  return new Promise( (resolve, reject) => {
    wx.chooseLocation({
      success(res) {
        resolve(res)
      }
    })
  })
}

/**
 * 弹窗授权功能 。 type 为 需求授权的类型
 */
export const accreditFlag = (type) => {
  return new Promise((resolve, reject) => {
    wx.authorize({
      scope: type,
      success() {
        resolve(true)
      },
      fail(err) {
        resolve(false)
      }
    })
  })
}


/**
 * 微信登录的code。可以用于获取用户的session_key，openid，unionid 。获取群id(openGId)。但是code只能使用一次
 */ 
export const obtainWxLoginCode = () => {
  return new Promise((resolve, reject) => {
    wx.login({
      success (res) {
        resolve(res)
      }
    })
  })
}

/**
 * 获取分享信息。返回的是encryptedData,iv。传给后台解析
 */ 
export const obtainShareInfo = (shareTicket) => {
  return new Promise((resolve, reject) => {
    wx.getShareInfo({
      shareTicket: shareTicket,
      success (res) {
        resolve(res)
      }
    })
  })
}

// 上传
export const chooseImage = (count = 1, sizeType = ['compressed'], sourceType = ['album', 'camera']) => {
  return new Promise((resolve, reject) => wx.chooseImage({ count, sizeType, sourceType, success: resolve, fail: reject }));
}

export const uploadFile = (url, filePath, name, formData = { openid: "test" }) => {
  return new Promise((resolve, reject) => {
    let opts = { url, filePath, name, formData, header: { 'Content-Type': "multipart/form-data" }, success: resolve, fail: reject };
    wx.uploadFile(opts);
  });
} 

export const chooseVideo = (compressed = true, sourceType = ['album', 'camera'], maxDuration = 60) => {
  return new Promise((resolve, reject) => wx.chooseVideo({ sourceType, compressed, maxDuration, success: resolve, fail: reject }));
}

